﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     main.cpp
// 描述:
// 作者:      颜磊
// 创建工程：   SpaceClear
// 创建时间:   2024-07-24 15:33
///////////////////////////////////////////////////////////////////////////
#include "widget.h"

#include <QApplication>
#include <QDateTime>
#include <QFile>
#include <QMutex>
#include <QSettings>
#include <QTextStream>
#include "mainservice.h"


void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    mutex.lock();
    //    //抛出位置
    //    QString context_info = QString("File:(%1) Line:(%2) Func:(%3)").arg(QString(context.file)).arg(context.line).arg(context.function);
    //    context_info="";
    QString logDataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString logData = QDateTime::currentDateTime().toString("yyyy-MM-dd");

    QString logHead;
    QString message;
    switch(type)
    {
    case QtDebugMsg:
        logHead = QString("运行");
        message = QString("<a style='color:green;'>【%1 | %2】: %3</a>").arg(logHead).arg(logDataTime).arg(msg);
        break;
    case QtWarningMsg:
        logHead = QString("警告");
        message = QString("<a style='color:yellow;'>【%1 | %2】: %3</a>").arg(logHead).arg(logDataTime).arg(msg);
        break;
    case QtCriticalMsg:
        logHead = QString("严重");
        message = QString("<a style='color:red;'>【%1 | %2】: %3</a>").arg(logHead).arg(logDataTime).arg(msg);
        break;
    case QtFatalMsg:
        logHead = QString("致命");
        message = QString("<a style='color:red;'>【%1 | %2】: %3</a>").arg(logHead).arg(logDataTime).arg(msg);
    }
    QString logPath = QString(qApp->applicationDirPath()+"/log/"+logData+"_log.html");

    QFile file(logPath);
    file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
    QTextStream text_stream(&file);
    text_stream.setCodec("utf-8");
    text_stream << message << "<br/>";
    file.flush();
    file.close();
    mutex.unlock();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSettings settings(QCoreApplication::applicationDirPath()+"/"+"config/service.ini",QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    bool logOut= true;
    if(settings.value("logParam/logOut").isNull()){
        logOut = true;
        settings.setValue("logParam/logOut",true);
    }else{
        logOut = settings.value("logParam/logOut").toBool();
    }
    if(logOut){
        qInstallMessageHandler(outputMessage);
    }
    bool isService= true;
    if(settings.value("APP/isService").isNull()){
        isService= true;
        settings.setValue("APP/isService",true);
    }else{
        isService = settings.value("APP/isService").toBool();
    }
    if(isService){
        MainService service(argc, argv);
        return service.exec();
    }else{
        Widget w;
        w.show();
        return a.exec();
    }
}
